﻿<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public class Upload : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];

            string savepath = "";
            string folder_name = context.Session["album_folder"].ToString();
            string tempPath = "~/files_upload/album/" + folder_name;
            savepath = context.Server.MapPath(tempPath);
            string filename = postedFile.FileName;

            //context.Session["fileName"] = filename;
            
            //Lưu vào Database
            DataAccess.ExeNonSQL("INSERT INTO dev_album_detail(idalbum,photo_name,folder_name) VALUES(" + Convert.ToInt32(context.Session["idalbum"]) + ",'" + filename + "','" + folder_name + "')");

            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);

            postedFile.SaveAs(savepath + "/" + filename);
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;

        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}